<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');
//print_r($HTTP_POST_VARS);
$shopList = Shop::GetAllShops();


if( $defaultShopId < 1 ){
	$defaultShopId = 1;
}

$timeTypeList = array(1 => "日", 2 => "週", 3 => "月");
$shop = Shop::getById($defaultShopId);

$tmp_date=date("YmdHi");
$tmp_year=substr($tmp_date,0,4) * 1;
$tmp_mon =substr($tmp_date,4,2) * 1;
$tmp_day = substr($tmp_date, 6, 2) * 1;
$tmp_Hour = substr($tmp_date, 8, 2) * 1;
$tmp_Minute = substr($tmp_date, 10, 2) * 1;

$searchDate = $_GET['search_date'];
$getDateArray = array();
if( preg_match("#/#", $searchDate) ){
	$getDateArray = explode("/", $searchDate);
}else if( preg_match("#-#", $searchDate) ){
	$getDateArray = explode("-", $searchDate);
}else{
	array_push($getDateArray, substr($searchDate, 0, 4));
	array_push($getDateArray, substr($searchDate, 4, 2));
	array_push($getDateArray, substr($searchDate, 6, 2));
}

$year = $getDateArray[0] * 1;
$month = $getDateArray[1] * 1;
$day = $getDateArray[2] * 1;

$year = $year > 0 ? $year : $tmp_year;
$month = $month > 0 ? $month : $tmp_mon;
$day = $day > 0 ? $day : $tmp_day;


$timeType = $_GET['time_type'];
$timeType = $timeType < 1 ? 1 : $timeType;

$startTime = mktime(0, 0, 0, $month, $day, $year);
$interval = array();
if( $timeType == 1 ){
	$endTime = mktime(23, 59, 59, $month, $day, $year);
	$daySub = 1;
	
	$interval[0] = 0;
}else if( $timeType == 2 ){
	$endTime = mktime(23, 59, 59, $month, $day + 7, $year);
	$daySub = 7;
	
	for($index = 0; $index < 8; $index++){
		$interval[$index] = $index;
	}
}else if( $timeType == 3 ){
	$endTime = mktime(23, 59, 59, $month + 1, $day, $year);
	$daySub = round($endTime - $startTime)/(3600*24);
	for($index = 0; $index < $daySub; $index++){
		$interval[$index] = $index;
	}
}

$workDayList = array();
$absenceDayList = array();

$staffList = Staff::GetAllByShop($defaultShopId);
foreach($staffList as $k => $v){
	$temp = array();
	$temp1 = array();
	
	for($index = 0; $index <= $daySub; $index++){
		$tempStartTime = mktime(0, 0, 0, $month, $day + $index, $year);
		$tempEndTime = mktime(23, 59, 59, $month, $day + $index, $year);
		
		$tempTemp  = WorkSchedule::getListbyTime($v['id'], $tempStartTime, $tempEndTime);
		Logs::setLog("".$tempTemp."||".$tempTemp['start_time']);
		array_push($temp, $tempTemp == null ? array() : $tempTemp );
		$tempTemp  = Absence::getListByTime($v['id'], $tempStartTime, $tempEndTime);
		array_push($temp1, $tempTemp == null ? array() : $tempTemp );
	}
	
	
	$workDayList[ $v['id'] ] = $temp;
	$absenceDayList[ $v['id'] ] = $temp1;
}
$searchUrl = "http://".$_SERVER ['HTTP_HOST'].$_SERVER['PHP_SELF']."?".$_SERVER[ 'QUERY_STRING'];
if( $_GET['response_type'] == 'print'){
	include template('/service/staff_work_list_print');
}else{
	include template('/service/staff_work_list');
}
